use "crps.dta"

xtset ccode year
foreach nvar of varlist cnaid_pop_ln-cnaidother_soe_gni_ln{
by ccode (year), sort: gen running_sum_var = sum(`nvar')
by ccode (year): gen running_count_var = sum(!missing(`nvar'))
by ccode (year): gen `nvar'3 = (running_sum_var - L3.running_sum_var)/(running_count_var-L3.running_count_var) if _n > 3
by ccode: replace `nvar'3 = running_sum_var/running_count_var if _n <= 3
drop running_sum_var running_count_var
}
///3-year averages of aid measures

tabulate year, generate(yr)
drop yr1 
**********************
///Table B.1
//Model1
xtreg  f.v2x_horacc cnaidene_gni_ln3 gdppcln wdi_gdpgr oda_gni_ln3 region_polity  i.year if income_categpry!=4, fe vce(r)  

//Model2
xtreg  f.v2x_horacc cnaidene_gni_ln3 gdppcln wdi_gdpgr oda_gni_ln3 region_polity ross_oil_netexpc ross_gas_netexpc polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model3
xtreg  f.v2x_horacc c.cnaidene_gni_ln3##c.p_polity2 gdppcln wdi_gdpgr oda_gni_ln3 region_polity ross_oil_netexpc ross_gas_netexpc polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model4
xtreg  f.d.v2x_horacc v2x_horacc cnaidene_gni_ln3 gdppcln wdi_gdpgr oda_gni_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4 , fe vce(r)    

//Model5
xtreg  f.e_dpi_checks cnaidene_gni_ln3 gdppcln wdi_gdpgr oda_gni_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model6
xtreg  f.v2x_veracc cnaidene_gni_ln3 gdppcln wdi_gdpgr oda_gni_ln3 region_polity ross_oil_netexpc ross_gas_netexpc polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model7
xtreg  f.d.v2x_veracc v2x_veracc cnaidene_gni_ln3 gdppcln wdi_gdpgr oda_gni_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4 , fe vce(r)    

//Model8
xtreg  f.e_polcomp cnaidene_gni_ln3 gdppcln wdi_gdpgr oda_gni_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)   

**********************
///Table B.2
//Model1
xtreg  f.v2x_horacc cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 region_polity  i.year if income_categpry!=4, fe vce(r)  

//Model2
xtreg  f.v2x_horacc cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 region_polity ross_oil_netexpc ross_gas_netexpc polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model3
xtreg  f.v2x_horacc c.cnaidene_pop_ln3##c.p_polity2 gdppcln wdi_gdpgr oda_pop_ln3 region_polity ross_oil_netexpc ross_gas_netexpc polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model4
xtreg  f.d.v2x_horacc v2x_horacc cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4 , fe vce(r)    

//Model5
xtreg  f.e_dpi_checks cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model6
xtreg  f.v2x_veracc cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 region_polity ross_oil_netexpc ross_gas_netexpc polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model7
xtreg  f.d.v2x_veracc v2x_veracc cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4 , fe vce(r)    

//Model8
xtreg  f.e_polcomp cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)   

**********************
///Table B.3
//Model1
xtreg  f.v2x_horacc cnaideneln3 cnaidotherln3  odacom_conmln3 region_polity  i.year if income_categpry!=4, fe vce(r)

//Model2 
xtreg  f.v2x_veracc cnaideneln3 cnaidotherln3  odacom_conmln3 region_polity  i.year if income_categpry!=4, fe vce(r) 

//Model3
xtreg  f.v2x_horacc cnaidene_gni3 cnaidother_gni3 odacom_conm_gni3 region_polity i.year if income_categpry!=4, fe vce(r)   

//Model4
xtreg  f.v2x_veracc cnaidene_gni3 cnaidother_gni3 odacom_conm_gni3 region_polity i.year if income_categpry!=4, fe vce(r)   

//Model5
xtreg  f.v2x_horacc count_other count_ene  odacom_conmln region_polity  i.year if income_categpry!=4, fe vce(r)

//Model6 
xtreg  f.v2x_veracc count_other count_ene  odacom_conmln region_polity  i.year if income_categpry!=4, fe vce(r)  

**********************
///Table B.4
//Model1 
xtreg  f.v2x_horacc cnaidene_pop_ln3 cnaidother_pop_ln3 odacom_conm_pop_ln3 region_polity year if income_categpry!=4, fe vce(r)   

//Model2
xtreg  f.v2x_veracc cnaidene_pop_ln3 cnaidother_pop_ln3 odacom_conm_pop_ln3 region_polity year if income_categpry!=4, fe vce(r)   

//Model3
xtreg  f.v2x_horacc l3.cnaidene_pop_ln3 l3.cnaidother_pop_ln3 l3.odacom_conm_pop_ln3 region_polity i.year if income_categpry!=4, fe vce(r)   

//Model4
xtreg  f.v2x_veracc l3.cnaidene_pop_ln3 l3.cnaidother_pop_ln3 l3.odacom_conm_pop_ln3 region_polity i.year if income_categpry!=4, fe vce(r)   

//Model5
xtivreg2 d.v2x_horacc d.cnaidene_pop_ln d.cnaidother_pop_ln d.odacom_conm_pop_ln d.region_polity yr* (ld.v2x_horacc= ll.v2x_horacc) if income_categpry!=4, fe robust first partial(yr*) 

//Model6
xtivreg2 d.v2x_veracc d.cnaidene_pop_ln d.cnaidother_pop_ln d.odacom_conm_pop_ln d.region_polity yr* (ld.v2x_veracc= ll.v2x_veracc) if income_categpry!=4, fe robust first partial(yr*) 

**********************
///Table B.5 data preparation
drop if income_categpry==4 
keep ccode year e_regionpol v2x_horacc v2x_veracc cnaidene_pop_ln3 cnaidene_gni_ln3 odacom_conm_pop_ln3 odacom_conm_gni_ln3 cnaidother_pop_ln3 cnaidother_gni_ln3 gdppcln wdi_gdpgr  ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock
save "forimp.dta", replace

///generate imputed data with multiple_imputation.R
clear
use "mdata.dta"

mi import flong, m(imp) id(ccode year id) imp(v2x_veracc-region_polity)
mi stset, clear 
mi xtset ccode year

//Model1
mi estimate , cmdok post: xtreg f.v2x_horacc cnaidene_pop_ln3 cnaidother_pop_ln3 odacom_conm_pop_ln3 region_polity i.year , fe vce(r)  

//Model2
mi estimate , cmdok post: xtreg f.v2x_veracc cnaidene_pop_ln3 cnaidother_pop_ln3 odacom_conm_pop_ln3   region_polity i.year , fe vce(r)  

//Model3
mi estimate , cmdok post: xtreg f.v2x_horacc cnaidene_pop_ln3 cnaidother_pop_ln3 odacom_conm_pop_ln3 gdppcln wdi_gdpgr ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year , fe vce(r)  

//Model4
mi estimate , cmdok post: xtreg f.v2x_veracc cnaidene_pop_ln3 cnaidother_pop_ln3 odacom_conm_pop_ln3  gdppcln wdi_gdpgr ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year , fe vce(r)  

**********************
///Table B.6
//Model1
xtreg  f.v2juhcind cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model2
xtreg  f.v2juncind cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model3
xtreg  f.v2juhccomp cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model4
xtreg  f.v2jucomp cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model5
xtreg  f.v2lgotovst cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model6
xtreg  f.v2lginvstp cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model7
xtreg  f.v2lgqstexp cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

**********************
///Table B.7
foreach nvar of varlist v2elirreg v2elintim v2elmulpar v2elfrfair {
gen `nvar'_fill=normprob(`nvar')
replace `nvar'_fill=l.`nvar'_fill if `nvar'_fill==. & year>=codingstart & year<=codingend & !(year>gapstart & year<gapend)
replace `nvar'_fill=0 if v2x_elecreg==0
replace `nvar'_fill=f.`nvar'_fill if `nvar'_fill==. & v2x_veracc!=.
}
//impute for non-election years

//Model1
xtreg  f.v2x_elecreg cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model2
xtreg  f.v2elembaut cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model3
xtreg  f.v2elembcap cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model4
xtreg  f.v2elirreg_fill cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model5
xtreg  f.v2elintim_fill cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model6
xtreg  f.v2elmulpar_fill cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model7
xtreg  f.v2elfrfair_fill cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model8
xtreg  f.v2elsuffrage cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model9
xtreg  f.v2psoppaut cnaidene_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

**********************
///Table B.8
//Model1
xtreg  f.v2x_horacc cnaid_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model2
xtreg  f.v2x_veracc cnaid_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model3
xtreg  f.v2x_horacc cnaidother_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  

//Model4
xtreg  f.v2x_veracc cnaidother_pop_ln3 gdppcln wdi_gdpgr oda_pop_ln3 ross_oil_netexpc ross_gas_netexpc region_polity polity2_stock i.year if income_categpry!=4, fe vce(r)  
